<?php

namespace Home\Controller;

use Think\Controller;
use think\Session;

class IndexController extends Controller
{
    public $customer_id = '';
    public $store_id = 0;

    /*
     * 初始化
     */
    public function _initialize()
    {
        if (session('?customer_id') && session('?STORE_ID')) {
            $this->customer_id = session('customer_id');//用户id
            $this->store_id = session('STORE_ID');//店铺id
        }
        $this->customer_id = "20181102115031877722";
        $this->store_id = '11';
    }

    /**
     * 店铺欢迎页
     */
    public function index()
    {
        /**
         *获取店铺基本信息
         */
        if ($this->store_id) {
            $store_info = M('store')->field('store_id,store_name,bg_image,logo')->where(['store_id' => $this->store_id])->find();
        }else{
            $store_info = "";
        }

        $this->assign("store_info", $store_info);
        $this->display();
    }

    /**
     * 店铺主页
     */
    public function main()
    {
        if ($this->store_id) {
            $cate_list = [];
            //店铺基本信息
            $store_info = M('store')->field('store_id,store_name,bg_image,logo')->where(['store_id' => $this->store_id])->find();

            //满减信息
            $manjian = M('promotion_manjian')->field('name')->where(['store_id' => $this->store_id])->select();
            if (empty($manjian)) {
                $manjian = '暂无满减活动！';
            } else {
                foreach ($manjian as $key => $value) {
                    $manjian[$key] = $value['name'];
                }
                $manjian = implode('、', $manjian);
            }

            //公告通知信息
            $notice = M('notice')->field('content')->where(['store_id' => $this->store_id, 'is_show' => 1])->find();
            $notice = $notice['content'];
            if (empty($notice)) $notice = '暂无公告通知消息！';

            //获取店铺所有分类导航和楼层菜品列表
            $category_nav = M('dishes_category')->field('id,name')->where(['store_id' => $this->store_id])->select();
            foreach ($category_nav as $k => $v) {
                $cate_list[$k]['cate'] = $v['name'];
                $cate_list[$k]['list'] = M('dishes')
                    ->field('id,name,price,image')
                    ->where(['store_id' => $this->store_id, 'category_id' => $v['id'], 'is_up' => 1, 'num' => ['gt', 0]])
                    ->select();

                //计算用户添加到购物车里每个菜品的数量
                foreach ($cate_list[$k]['list'] as $k1 => $v1) {
                    $cate_list[$k]['list'][$k1]['cart_dishes_num'] = M('car')
                        ->where(['store_id' => $this->store_id, 'good_id' => $v1['id'], 'customer_id' => $this->customer_id])
                        ->getField('count');
                }
            }

//            echo M()->_sql();
//            echo '<pre>';
//            print_r($cate_list);
//            die;
        } else {
            $this->error("服务器繁忙请稍后再试！", U('Home/Index/index'));
        }

        $this->assign("store_info", $store_info);
        $this->assign("manjian", $manjian);
        $this->assign("notice", $notice);
        $this->assign("cate_list", $cate_list);
        $this->display();
    }
}